<template>
    <div class="crm-project-category-container">
        <el-dialog title="新增项目业务分类" v-model="isShowDialog" :close-on-click-modal="false" width="65%">
            <el-form ref="addCrmProjectCategoryFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8">
                        <el-form-item label="业务类别" prop="category">
                            <Dictionary v-model="formData.category" dict-label="crm_project_category" placeholder="请选择业务类别"></Dictionary>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8">
                        <el-form-item label="业务名称" prop="name">
                            <el-input v-model="formData.name" placeholder="请输入业务名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8">
                        <el-form-item label="提成金额" prop="quotaAmount">
                            <el-input-number v-model="formData.quotaAmount" placeholder="请输入提成金额（单位：元）" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <p class="color-warning mb15">按住鼠标左键可拖拽排序步骤顺序</p>
                        <draggable :list="formData.steps">
                            <template #item="{ element,index }">
                                <el-row :gutter="35">
                                    <el-col :xs="1" :sm="1" :md="1" :lg="1" :xl="1">
                                        <el-button type="primary" icon="el-icon-plus" circle @click="onAddRow" v-if="index === 0"></el-button>
                                        <el-button type="danger" icon="el-icon-delete" circle @click="onDelRow(index)" v-else></el-button>
                                    </el-col>
                                    <el-col :xs="24" :sm="9" :md="9" :lg="9" :xl="9">
                                        <el-form-item label="步骤名称" :rules="[{ required: true, message: `步骤名称不能为空`, trigger: 'blur' }]">
                                            <el-input v-model="element.stepName" placeholder="请输入步骤名称"> </el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :xs="24" :sm="9" :md="9" :lg="9" :xl="9">
                                        <el-form-item label="步骤内容" :rules="[{ required: true, message: `步骤内容不能为空`, trigger: 'blur' }]">
                                            <el-input v-model="element.stepText" placeholder="请输入步骤内容"> </el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :xs="23" :sm="5" :md="5" :lg="5" :xl="5">
                                        <el-form-item label="工时(天)" :rules="[{ required: true, message: `所需人工时（天）不能为空`, trigger: 'blur' }]">
                                            <el-input-number v-model="element.stepDaytime" :min="0" :step="1" step-strictly placeholder="请输入所需人工时（天）" class="w100"> </el-input-number>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </template>
                        </draggable>
                    </el-col>
                    <el-col>
                        <el-form-item label="业务要求" prop="requirement">
                            <el-input type="textarea" :rows="5" v-model="formData.requirement" placeholder="请输入业务要求" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="办理结果" prop="handleResult">
                            <el-input type="textarea" :rows="5" v-model="formData.handleResult" placeholder="请输入办理结果" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import draggable from 'vuedraggable';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addCrmProjectCategory } from '/@/api/crm/project/category/index';
import Dictionary from '/@/components/dictionary/index.vue';
export default {
    name: 'crm/project/category/add',
    components: { Dictionary, draggable },
    setup(props, context) {
        const addCrmProjectCategoryFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {
                steps: [],
            },
            formRules : {
                category: { required: true, message: '业务类别不能为空', trigger: 'blur' },
                name: { required: true, message: '业务名称不能为空', trigger: 'blur' },
                quotaAmount: { required: true, message: '提成金额不能为空', trigger: 'blur' },
                requirement: { required: true, message: '业务要求不能为空', trigger: 'blur' },
            }
        });

        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            onAddRow();
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addCrmProjectCategoryFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addCrmProjectCategoryFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        category: state.formData.category,
                        name: state.formData.name,
                        quotaAmount: state.formData.quotaAmount,
                        requirement: state.formData.requirement,
                        handleResult: state.formData.handleResult,
                        remark: state.formData.remark,
                        steps: state.formData.steps,
                    }
                    addCrmProjectCategory(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        // 新增行
		const onAddRow = () => {
			state.formData.steps.push({
				categoryId: state.formData.id,
                stepNumber: state.formData.steps.length + 1,
				stepName: '',
				stepText: '',
				stepDaytime: '1',
			});
		};
		// 删除行
		const onDelRow = (k) => {
			state.formData.steps.splice(k, 1);
		};
        // 阻止部分浏览器默认拖拽行为
		document.body.ondrop = (event) => {
			event.preventDefault();
			event.stopPropagation();
		};
        return {
            addCrmProjectCategoryFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            onAddRow,
            onDelRow,
            ...toRefs(state),
        };
    },
};
</script>